package com.axend.aerosense.dev.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.common.bean.RadarBean;
import com.axend.aerosense.common.bean.RadarType;
import com.axend.aerosense.common.ui.CustomToolBar;
import com.axend.aerosense.dev.fragment.DevBedAndBreatheFragment;
import com.axend.aerosense.dev.fragment.DevAssureCheckRoomFragment;
import com.axend.aerosense.dev.fragment.DevDeviceSettingsFragment;
import com.axend.aerosense.dev.impl.IBack;
import com.axend.aerosense.dev.impl.ICustomToolBar;
import com.axend.aerosense.dev.impl.IFirstFragment;
import com.gyf.immersionbar.ImmersionBar;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevActivityDevicesMainBinding;
import com.axend.aerosense.dev.fragment.DevSearchNetFragment;


@Route(path = RouterActivityPath.Dev.PAGE_DEV_HOME)
public class NewDevSetNetActivity extends DevBaseWifiActivity<DevActivityDevicesMainBinding, BaseViewModel>
    implements IFirstFragment, ITitle, ICustomToolBar {

    @Autowired
    RadarBean radarBean;
    private Fragment firstFragment;
    private IBack iBack;
    private boolean enableBack = true;
    private boolean isFirstFragment = false;

    public boolean isFirstFragment() {
        return isFirstFragment;
    }

    public void setFirstFragment(boolean firstFragment) {
        isFirstFragment = firstFragment;
    }

    @Override
    public Fragment getFirstFragment(){
        return firstFragment;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true).init();
        setSupportActionBar(viewDataBinding.devToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());
        if (null == radarBean){
            firstFragment = DevSearchNetFragment.getInstance();
            FragmentUtil.add(this, viewDataBinding.devContent.getId(), firstFragment, null, false);
        }else {
            if(radarBean.getType().get() == RadarType.WAVVE.get() || radarBean.getType().get() == RadarType.WAVVE_PRO.get()){
                firstFragment = DevBedAndBreatheFragment.getInstance(radarBean);
                FragmentUtil.add(this, viewDataBinding.devContent.getId(), firstFragment, null, false);

            }else if (radarBean.getType().get() == RadarType.ASSURE.get() ||  radarBean.getType().get() == RadarType.ASSURE_LITE.get()){
                firstFragment = DevDeviceSettingsFragment.getInstance(radarBean);
                FragmentUtil.add(this, viewDataBinding.devContent.getId(), firstFragment, null, false);
            }

        }

//        FragmentUtils.add(getSupportFragmentManager(),
//                firstFragment,
//                viewDataBinding.devContent.getId(),
//                firstFragment.getClass().getSimpleName(),
//                false,
//                com.axend.aerosense.common.R.anim.common_in_from_left,
//                com.axend.aerosense.common.R.anim.common_out_to_right,
//                com.axend.aerosense.common.R.anim.common_in_from_left,
//                com.axend.aerosense.common.R.anim.common_out_to_right);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (!enableBack){
            return;
        }
        if (iBack != null){
            if (iBack.onBack()){
                return;
            }
        }
        if(getSupportFragmentManager().getBackStackEntryCount() == 0){
            finish();
            return;
        }
        if(isFirstFragment){
            FragmentManager.BackStackEntry entry = this.getSupportFragmentManager().getBackStackEntryAt(0);
            int id = entry.getId();
            this.getSupportFragmentManager().popBackStack(id, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            return;
        }
        super.onBackPressed();
    }

    /** 是否启用返回 */
    public void enableBack(boolean enable){
        getSupportActionBar().setHomeButtonEnabled(enable);
        getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
        enableBack = enable;
    }

    /** 是否显示右边文字 */
    public void isShowRightText(boolean b, OnCompleteBtnListener listener){
        if (b){
            viewDataBinding.devRightText.setText(getString(R.string.common_complete));
            viewDataBinding.devRightText.setVisibility(View.VISIBLE);
            int width = (int) (getWindowManager().getDefaultDisplay().getWidth() * 0.62f);
            viewDataBinding.devTitle.setMaxWidth(width);
            viewDataBinding.devTitle.setSingleLine(true);
            viewDataBinding.devTitle.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
            viewDataBinding.devRightText.setOnClickListener(v->{
                if (listener != null){
                    listener.onClick(v);
                }
            });
        }else {
            viewDataBinding.devRightText.setVisibility(View.GONE);
        }
    }

    public void setiBack(IBack iBack){
        this.iBack = iBack;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(null != this.getCurrentFocus()){
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void setTitle(String title){
        viewDataBinding.devTitle.setText(title);
    }

    private FragmentManager.OnBackStackChangedListener getListener() {
        FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                FragmentManager manager = getSupportFragmentManager();
                if (manager != null) {
                    Fragment currFrag = (Fragment) manager.findFragmentById(getContentId());
                    currFrag.onResume();
                }
            }
        };

        return result;
    }
    @Override
    public int getContentId(){
        return viewDataBinding.devContent.getId();
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dev_activity_devices_main;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public CustomToolBar getCustomToolBar() {
        return viewDataBinding.devToolbar;
    }

    /**
     * 完成按钮点击回调
     */
    public interface OnCompleteBtnListener{
        void onClick(View view);
    }


    public void setRightTitle(String title) {
        viewDataBinding.devToolbar.setMySettingText(title);
        if (!TextUtils.isEmpty(title)) {
            viewDataBinding.devToolbar.setRightTitleVisibility(View.VISIBLE);
        } else {
            viewDataBinding.devToolbar.setRightTitleVisibility(View.GONE);
        }
    }

    public void setRightTitleClickable(boolean clickable) {
        if(clickable){
            viewDataBinding.devToolbar.setMySettingTextColor(getResources().getColor(R.color.common_item_selector, null));
        }else{
            viewDataBinding.devToolbar.setMySettingTextColor(getResources().getColor(R.color.common_btn_pressed_color_gray, null));
        }
        viewDataBinding.devToolbar.setRightTitleClickable(clickable);
    }

    public CustomToolBar getToolBar(){
        return viewDataBinding.devToolbar;
    }

}
